<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8" />
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0" />
        <title>Document</title>
    </head>
    <body>
        <script>
            //这样有个问题，如果grid==[[1]]，无法判断岛屿数量为1的情况
            var maxAreaOfIsland = function (grid) {
                let count = 0
                let result = 0
                let queue = []
                let m = grid.length
                let n = grid[0].length
                for (let i = 0; i < m; i++) {
                    for (let j = 0; j < n; j++) {
                        if (grid[i][j] == "1") {
                            count = 0
                            //bfs还得避免重复
                            grid[i][j] == "0"
                            queue.push([i, j])
                            bfs(queue)
                            result = Math.max(result, count)
                        }
                    }
                }
                return result

                function bfs(queue) {
                    let dirs = [
                        [1, 0],
                        [-1, 0],
                        [0, 1],
                        [0, -1],
                    ]
                    while (queue.length) {
                        let cur = queue.shift()
                        //添一种情况
                        if (grid[(cur[0], cur[1])] == "1") {
                            count++
                        }
                        for (let dir of dirs) {
                            let x = cur[0] + dir[0]
                            let y = cur[1] + dir[1]
                            if (x < 0 || y < 0 || x >= m || y >= n || grid[x][y] == 0) {
                                continue
                            }
                            count++
                            //这里都是先置为0再推入的，所以if情况只会有一次
                            grid[x][y] = "0"
                            queue.push([x, y])
                        }
                    }
                }
            }
            console.log(maxAreaOfIsland([[0, 1]]))
        </script>
    </body>
</html>
